//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.weibo.platform.netease.api.timeline
{
	import com.rogo.weibo.core.DoAPI;
	import com.rogo.weibo.platform.netease.api.oauth.Oauth;
	import com.rogo.weibo.utils.HttpParameter;
	import com.rogo.weibo.utils.OauthUrlUtil;
	
	import flash.net.URLRequestMethod;
	
	import json.JSON;
	
	/**
	 * 网易微博API 
	 * 1.1.1 微博列表
	 * 
	 * @see http://rogo.sinaapp.com
	 * @see http://open.t.163.com
	 * @author rogo
	 */	
	public class DoTimeline extends DoAPI
	{
		public function DoTimeline(dataHandler:Function, errorHandler:Function)
		{
			super(dataHandler, errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		private static const URL_HOME_TIMELINE:String = 'http://api.t.163.com/statuses/home_timeline.json';
		public static const CMD_HOME_TIMELINE:String = 'cmdHomeTimeline';
		
		private static const URL_PUBLIC_TIMELINE:String = 'http://api.t.163.com/statuses/public_timeline.json';
		public static const CMD_PUBLIC_TIMELINE:String = 'cmdPublicTimeline';
		
		private static const URL_MENTIONS:String = 'http://api.t.163.com/statuses/mentions.json';
		public static const CMD_MENTIONS:String = 'cmdMentions';
		
		private static const URL_USER_TIMELINE:String = 'http://api.t.163.com/statuses/user_timeline.json';
		public static const CMD_USER_TIMELINE:String = 'cmdUserTimeline';
		
		private static const URL_RETWEET_OF_ME:String = 'http://api.t.163.com/statuses/retweets_of_me.json';
		public static const CMD_RETWEET_OF_ME:String = 'cmdRetweetOfMe';
		
		private static const URL_GET_COMMENTS_BY_ME:String = 'http://api.t.163.com/statuses/comments_by_me.json';
		public static const CMD_GET_COMMENTS_BY_ME:String = 'cmdGetCommentsByMe';
		
		private static const URL_GET_COMMENTS_TO_ME:String = 'http://api.t.163.com/statuses/comments_to_me.json';
		public static const CMD_GET_COMMENTS_TO_ME:String = 'cmdGetCommentsToMe';
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 获取当前登录用户关注用户的最新微博列表
		 * 
		 * @param count 数量，默认为30条，最大为200条
		 * @param since_id 该参数需传cursor_id,返回此条索引之前发的微博列表,不包含此条
		 * @param max_id 该参数需传cursor_id,返回此条索引之后发的微博列表,包含此条
		 * @param trim_user 值为true时返回的user对象只包含id属性，该属性能在一定程度上减少返回的数据量
		 */		
		public function getHomeTimeline(count:String, since_id:String, max_id:String, trim_user:Boolean):void
		{
			var paras:Array = [];
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			
			if (since_id != null && since_id != '')
				paras.push(new HttpParameter('since_id', since_id));
			
			if (max_id != null && max_id != '')
				paras.push(new HttpParameter('max_id', max_id));
			
			paras.push(new HttpParameter('trim_user', trim_user.toString()));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_HOME_TIMELINE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_HOME_TIMELINE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_HOME_TIMELINE, params);
			}
		}
		
		/**
		 * 获取最新的公共微博列表(随便看看)
		 * 
		 * @param trim_user 值为true时返回的user对象只包含id属性
		 */		
		public function getPublicTimeline(trim_user:Boolean):void
		{
			var paras:Array = [];
			paras.push(new HttpParameter('trim_user', trim_user.toString()));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_PUBLIC_TIMELINE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_PUBLIC_TIMELINE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_PUBLIC_TIMELINE, params);
			}
		}
		
		/**
		 * 获取@评论当前登录用户的微博列表
		 * 
		 * @param count 数量，默认为30条，最大为200条
		 * @param since_id 该参数需传cursor_id,返回此条索引之前发的微博列表,不包含此条
		 * @param max_id 该参数需传cursor_id,返回此条索引之后发的微博列表，包含此条
		 * @param trim_user 值为true时返回的user对象只包含id属性，该属性能在一定程度上减少返回的数据量
		 */		
		public function getMentions(count:String, since_id:String, max_id:String, trim_user:Boolean):void
		{
			var paras:Array = [];
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			
			if (since_id != null && since_id != '')
				paras.push(new HttpParameter('since_id', since_id));
			
			if (max_id != null && max_id != '')
				paras.push(new HttpParameter('max_id', max_id));
			
			paras.push(new HttpParameter('trim_user', trim_user.toString()));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_MENTIONS, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_MENTIONS, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_MENTIONS, params);
			}
		}
		
		/**
		 * 获取当前登录用户关注用户的最新微博列表
		 * 
		 * @param user_id 可选参数 主要是用来区分用户ID跟微博昵称相同产生歧义的情况
		 * @param screen_name 可选参数 可以传user_id或screen_name
		 * @param name 可选参数 用户昵称
		 * @param count 可选参数 数量，默认为30条，最大为200条
		 * @param since_id 可选参数 该参数需传cursor_id,返回此条索引之前发的微博列表,不包含此条
		 * @param max_id 可选参数 该参数需传cursor_id,返回此条索引之后发的微博列表,包含此条
		 * @param trim_user 可选参数 值为true时返回的user对象只包含id属性，该属性能在一定程度上减少返回的数据量
		 */		
		public function getUserTimeline(user_id:String, screen_name:String, name:String, 
										count:String, since_id:String, max_id:String, trim_user:Boolean):void
		{
			var paras:Array = [];
			if (user_id != null && user_id != '')
				paras.push(new HttpParameter('user_id', user_id));
			if (screen_name != null && screen_name != '')
				paras.push(new HttpParameter('screen_name', screen_name));
			if (name != null && name != '')
				paras.push(new HttpParameter('name', name));
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			if (since_id != null && since_id != '')
				paras.push(new HttpParameter('since_id', since_id));
			if (max_id != null && max_id != '')
				paras.push(new HttpParameter('max_id', max_id));
			
			paras.push(new HttpParameter('trim_user', trim_user.toString()));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_USER_TIMELINE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_USER_TIMELINE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_USER_TIMELINE, params);
			}
		}
		
		/**
		 * 获取当前登录用户所发微博被转发的列表
		 * 
		 * @param count 可选参数，数量，默认为30条，最大为200条
		 * @param since_id 可选参数，该参数需传cursor_id,返回此条索引之前发的微博列表,不包含此条
		 */		
		public function getRetweetsOfMe(count:String, since_id:String):void
		{
			var paras:Array = [];
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			if (since_id != null && since_id != '')
				paras.push(new HttpParameter('since_id', since_id));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_RETWEET_OF_ME, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_RETWEET_OF_ME, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_RETWEET_OF_ME, params);
			}
		}
		
		/**
		 * 获取我发出的评论列表
		 * 
		 * @param count 可选参数，数量，默认为30条，最大为200条
		 * @param since_id 可选参数，该参数需传cursor_id,返回此条索引之前发的微博列表,不包含此条
		 * @param max_id 可选参数，该参数需传cursor_id,返回此条索引之后发的微博列表，包含此条
		 * @param trim_user 可选参数，值为true时返回的user对象只包含id属性，该属性能在一定程度上减少返回的数据量。默认为true
		 */		
		public function getCommentsByMe(count:String, since_id:String, max_id:String, trim_user:Boolean):void
		{
			var paras:Array = [];
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			
			if (since_id != null && since_id != '')
				paras.push(new HttpParameter('since_id', since_id));
			
			if (max_id != null && max_id != '')
				paras.push(new HttpParameter('max_id', max_id));
			
			paras.push(new HttpParameter('trim_user', trim_user.toString()));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_GET_COMMENTS_BY_ME, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GET_COMMENTS_BY_ME, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GET_COMMENTS_BY_ME, params);
			}
		}
		
		/**
		 * 获取我收到的评论列表
		 * 
		 * @param count 可选参数，数量，默认为30条，最大为200条
		 * @param since_id 可选参数，该参数需传cursor_id,返回此条索引之前发的微博列表,不包含此条
		 * @param max_id 可选参数，该参数需传cursor_id,返回此条索引之后发的微博列表，包含此条
		 * @param trim_user 可选参数，值为true时返回的user对象只包含id属性，该属性能在一定程度上减少返回的数据量。默认为true
		 */		
		public function getCommentsToMe(count:String, since_id:String, max_id:String, trim_user:Boolean):void
		{
			var paras:Array = [];
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			
			if (since_id != null && since_id != '')
				paras.push(new HttpParameter('since_id', since_id));
			
			if (max_id != null && max_id != '')
				paras.push(new HttpParameter('max_id', max_id));
			
			paras.push(new HttpParameter('trim_user', trim_user.toString()));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_GET_COMMENTS_TO_ME, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GET_COMMENTS_TO_ME, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GET_COMMENTS_TO_ME, params);
			}
		}
		
		private function resultResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_dataHandler(cmd, data);
		}
		
		private function errorResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_errorHandler(cmd, data);
		}
		
	}
}